<?php
//
namespace app\circle\controller\admin;

use app\circle\model\MeetingContentModel;
use app\circle\model\MeetingModel;
use think\Request;

class MeetingContentController extends BaseController
{

    private $meetingModel;
    private $meetingContentModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->meetingModel        = new MeetingModel();
        $this->meetingContentModel = new MeetingContentModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        $query      = $this->request->request();
        $meetingName  = $this->request->param('meeting_name');
        if (!empty($meetingName)) {
            $map['meeting_name'] = ['like', '%' . $meetingName . '%'];
        }
        if ($this->adminInfo['admin_power']==2){
            $map['dept_id']=$this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power']==3){
            $map['admin_id']=$this->adminInfo['admin_id'];
        }
        $list = $this->meetingContentModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'meeting_id|会议'  => 'require',
                'title|标题' => 'require',
                'content|内容' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $meetingInfo = $this->meetingModel->where('meeting_id',$data['meeting_id'])->find();
            if(empty($meetingInfo)){
                $this->error('绑定会议不存在！');
            }
            $data['meeting_name'] = $meetingInfo['meeting_name'];
            $data['dept_id'] = $this->adminInfo['dept_id'];
            $data['dept_name'] = $this->adminInfo['dept_name'];
            $data['dept_path'] = $this->adminInfo['dept_path'];
            $data['admin_id'] = $this->adminInfo['admin_id'];
            $this->meetingContentModel->addData($data);
            $this->success('操作成功！','index');
        }
        $map        = [];
        if ($this->adminInfo['admin_power']==2){
            $map['dept_id']=$this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power']==3){
            $map['admin_id']=$this->adminInfo['admin_id'];
        }
        $meeting = $this->meetingModel->where($map)->select();
        $this->assign('meeting',$meeting);
        return $this->fetch();
    }
    /**
     * 编辑
     */
    public function edit(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'meeting_id|会议'  => 'require',
                'title|标题' => 'require',
                'content|内容' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $meetingInfo = $this->meetingModel->where('meeting_id',$data['meeting_id'])->find();
            if(empty($meetingInfo)){
                $this->error('绑定会议不存在！');
            }
            $data['meeting_name'] = $meetingInfo['meeting_name'];
            $this->meetingContentModel->editData($data);
            $this->success('操作成功！','index');
        }
        $id = $this->request->param('id/d');
        $info = $this->meetingContentModel->where('id',$id)->find();
        $map        = [];
        if ($this->adminInfo['admin_power']==2){
            $map['dept_id']=$this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power']==3){
            $map['admin_id']=$this->adminInfo['admin_id'];
        }
        $meeting = $this->meetingModel->where($map)->select();
        $this->assign('meeting',$meeting);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('id/d');
        $this->meetingContentModel->where('id',$id)->delete();
        $this->success('删除成功！');
    }

}
